<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_invoice {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_invoice inv LEFT JOIN tbl_user u ON inv.user_id = u.user_id "
                . "LEFT JOIN tbl_inv_status ins ON inv.inv_status_id = ins.inv_status_id";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function get_all_unpaid() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_invoice inv LEFT JOIN tbl_user u ON inv.user_id = u.user_id "
                . "LEFT JOIN tbl_inv_status ins ON inv.inv_status_id = ins.inv_status_id WHERE ins.inv_status_id = 1";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function get_all_paid() {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_invoice inv LEFT JOIN tbl_user u ON inv.user_id = u.user_id "
                . "LEFT JOIN tbl_inv_status ins ON inv.inv_status_id = ins.inv_status_id WHERE ins.inv_status_id = 2";
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query)
        or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();
        return $list;
    }

    public function save($invoice_id, $user_id, $created_date, $inv_status_id, $total_money, $total_quantity) {
        $db = new connection();
        $con = $db->open_connect();

        if ($invoice_id == 0) {
            $query = "INSERT INTO tbl_invoice(user_id, created_date, inv_status_id, total_money, total_quantity) VALUES (
                '" . $user_id . "','" . $created_date . "'," . $inv_status_id . "," . $total_money . "," . $total_quantity . ")";
        } else {
            $query = "UPDATE tbl_invoice SET "
                    . "user_id = '" . $user_id . "',"
                    . "created_date = '" . $created_date . "',"
                    . "inv_status_id = " . $inv_status_id . ","
                    . "total_money = " . $total_money . ","
                    . "total_quantity = " . $total_quantity . " "
                    . "WHERE invoice_id = " . $invoice_id;
        }
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $inserted_id = mysqli_insert_id($db->con);
        $db->close_connect();
        return $inserted_id;
    }

    public function get_by_id($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "SELECT inv.*,u.first_name AS customer_name, u.email AS customer_email,"
                . "u.phone AS customer_phone, ins.inv_status_name AS status_name "
                . "FROM tbl_invoice inv LEFT JOIN tbl_user u ON inv.user_id = u.user_id "
                . "LEFT JOIN tbl_inv_status ins ON inv.inv_status_id = ins.inv_status_id "
                . "WHERE invoice_id = " . $id;
        /** @noinspection PhpParamsInspection */
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();
        return $row;
    }

    public function delete($id) {
        $db = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_invoice WHERE invoice_id = " . $id;
        /** @noinspection PhpParamsInspection */
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();
        return TRUE;
    }
}
